home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / htable.zip / DEMOTAB.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-04  |  5KB  |  117 lines

  1. Unit DemoTab;  {Demonstration for Application Hash Tables}
  2. {$R-,S-,V-}    {See GenTable.Doc for indepth explanations}
  3.                {Simply copy this Unit, changing all Demo }
  4.                {declarations to whatever you want to make}
  5.                {a Hash table for.                        }
  6.  
  7. INTERFACE
  8.  
  9. Uses GenTable;
  10.  
  11. Type
  12.   Demo = Record
  13.           Data : LongInt
  14.         End;
  15.  
  16.   {NOTE: Potential problems with data records which contain fields }
  17.   {      which are pointers.  There is NO provision for copying any}
  18.   {      data referenced by the pointer -- only the pointer itself }
  19.   {      will be copied into the table.  If you then destroy your  }
  20.   {      "local copy" of the referenced data, it is GONE, and can  }
  21.   {      not be recovered by the entry in the table.  Worse yet,   }
  22.   {      this error is completely undetectable by the table, and it}
  23.   {      will lead to all manner of misleading error messages. Boy }
  24.   {      do I know this one -- I had such an error in a simple     }
  25.   {      80x86 assembler MacroTable which drove me buggy for weeks!}
  26.  
  27.   DemoTable = Object (HTable)
  28.  
  29. { By redefining the following, we can (come as close as possible to)    }
  30. { impose Data Types on the Generic Hash Table.  It should be noted that }
  31. { it IS possible to load differing Data Types into the same Hash Table, }
  32. { provided they have the exact same size.  while this SHOULD present no }
  33. { problems, it may (possibly) have some interesting uses....            }
  34.  
  35.                    Constructor Create (MaxEntries : LongInt;
  36.                                        LoadFactor : Word);
  37.  
  38.                    Function Hash (TheString : String) : Word; Virtual;
  39.  
  40.                    Procedure Enter (TheKey : String; D : Demo;
  41.                                          Var Duplicate : Boolean);
  42.  
  43.                    Procedure Retrieve (TheKey : String; Var D : Demo;
  44.                                                     Var Found : Boolean);
  45.  
  46.                    Procedure UpDate (TheKey : String; NewData : Demo);
  47.  
  48.                    Destructor Destroy; Virtual;
  49.  
  50. { Inherited HTable methods which require no changes }
  51. (*
  52.                    Function Member (Key : String) : Boolean; {Is key in Table?}
  53.  
  54.                    Function StaticLength : Word; {How many buckets allocated?}
  55.  
  56.                    Function Empty          : Boolean; {Is Table empty?}
  57.                    Function EntryCount     : Word;    {How many entries?}
  58.                    Function MaxLoad        : Byte;    {Size of largest bucket}
  59.                    Function Buckets_In_Use : Word;    {How many buckets used}
  60.                    Function AverageLoad    : Real;    {Average bucket size}
  61.                    Function LastBucket     : Word;    {Index of last bucket}
  62.                                                       { 1..StaticLength}
  63.                    Procedure Distribution_Report;     {Write certain stats}
  64.                                                       {to the screen}
  65. *)
  66.               End;
  67.  
  68. IMPLEMENTATION
  69.  
  70. Constructor DemoTable.Create (MaxEntries : LongInt; LoadFactor : Word);
  71. { Instantiates a Generic HashTable  }
  72. { "Typed" for the DemoRec Data Type }
  73. Begin
  74.   HTable.Create (LoadFactor,SizeOf(Demo),MaxEntries)
  75. End;
  76.  
  77. Function DemoTable.Hash (TheString : String) : Word;
  78. { If you use a different Hashing function, You MUST ensure }
  79. { that it returns a value from 0..HTable.StaticLength-1,   }
  80. { or you guarantee a system crash at some point!!!         }
  81. Begin
  82.   Hash := HTable.Hash(TheString)
  83. End;
  84.  
  85. Procedure DemoTable.Enter (TheKey : String; D : Demo;
  86.                                   Var Duplicate : Boolean);
  87. { Enters the data (of type Demo) into the table }
  88. { if Duplicate, then nothing was entered.       }
  89. Begin
  90.   HTable.Enter (TheKey,D,SizeOf(D),Duplicate)
  91. End;
  92.  
  93. Procedure DemoTable.Retrieve (TheKey : String; Var D : Demo;
  94.                                               Var Found : Boolean);
  95. { Retrieves the Demo associated with TheKey. }
  96. { If Not found, then nothing is done to D -- }
  97. { If D contained old Data, it still will!    }
  98. Begin
  99.   HTable.Retrieve (TheKey,D,SizeOf(D),Found)
  100. End;
  101.  
  102. Procedure DemoTable.UpDate (TheKey : String; NewData : Demo);
  103. { Allows the Data associated with TheKey to be changed }
  104. Begin
  105.   HTable.UpDate (TheKey,NewData,SizeOf(Demo))
  106. End;
  107.  
  108. Destructor DemoTable.Destroy;
  109. { Returns every last byte of the table (other then Data Segment }
  110. { space allocated for the Object variable itself) to the Heap   }
  111. Begin
  112.   HTable.Destroy
  113. End;
  114.  
  115. BEGIN
  116. {No Initialization Code}
  117. END.